<%@page import="java.util.LinkedList"%>
<%@page import="BusinessLogic.UserContainer"%>
<%@page import="BusinessLogic.User"%>
<%@page import="BusinessLogic.Result.ResultType"%>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="BusinessLogic.Result" %>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="public/style.css" rel="stylesheet" type="text/css" media="screen"/>
<title>EasySchedule</title>
<script type="text/javascript" src="public/jquery.js"></script>
<script type="text/javascript">
	function buttonClick(name,letter){
		$.get(
			    "/tamirandevyatar/usersValue",
			    {letter:letter},
			    function(data) { $('#users').html(data); },
			    "html"
			);

	}
	function showCourseDetails(cName){
		$('#'+cName).toggleClass('adminkey');
	}
	function deleteUser(uname,me){
		var answer = confirm ("Are you sure you want to delete this user?");
		if (answer)
			{
			$('#div'+uname).remove();
			var prevnum=$('#'+uname.toLowerCase().charAt(0)+'button').text();
			var num=prevnum.substring(2,prevnum.length-1);
			num=parseInt(num)-1;
			$('#'+uname.toLowerCase().charAt(0)+'button').text(prevnum.substring(0,2)+num+')');
			$.get(
				    "/tamirandevyatar/requestHandler",
				    {action:"deleteUser", USER_NAME:uname, me:me},
				    function(data) { 
				    				 data=data+'';
				    			if (data.indexOf("success",0)<0) alert(data); 
				    },
				    "html"
				);
			}
	}
</script>
</head>
<jsp:useBean id="requestHandler" class="RequestHandlerPackage.RequestHandler" scope="application"/>
<%
Map<String,String[]> params = new HashMap<String,String[]>();
params.put("action", new String[]{"getAllUsers"});
Result res = requestHandler.handle(params);
Map<String,Integer> userCount = new HashMap<String,Integer>();
String[] letters = new String[]{"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t",
		"u","v","w","x","y","z"};
for(int i=0;i<letters.length;i++)
	userCount.put(letters[i], 0);
if (res.getResultType().equals(ResultType.SUCCESS))
{
	UserContainer cont = (UserContainer)res.getSuccessReturnValue();
	for(int i=0;i<letters.length;i++)
		userCount.put(letters[i], cont.numberOfUsersIn(letters[i]));
}
String uName = request.getUserPrincipal().getName();
params.put("action",new String[]{"getUser"});
params.put("USER_NAME",new String[]{uName});
params.put("me",new String[]{uName});
res = requestHandler.handle(params);
boolean admin = false;
if (res.getResultType().equals(ResultType.SUCCESS))
{
	if (((User)(res.getSuccessReturnValue())).getRole().equals("admin")){
	  	admin = true;
	}
}
%>
	<body>
	   <div id="page">
	        <div id="header">
	            <h1><a href="./">Easy<span class="logo">Schedule</span></a></h1>
	            <p id="subtitle">Nice and easy registration system</p>
	        </div>
	        <div id="menu">
	            <ul>
	                <li class="left_menu"><a href="./">Home</a></li>
	                <li class="left_menu"><a href="./about.html">About</a></li>
	                <li class="left_menu"><a href="/tamirandevyatar/logout">Logout</a></li>
	            </ul>
	        </div>
	        <div id="main">	
	            <div id="sidebar">
	                <div class="box" id="sidebar_head">
	                <%if (admin){ %>
	                    <h2><a id="loginAnchor">Admin</a></h2>
	                     <%}else{ %>
	                      <h2><a id="loginAnchor">User</a></h2>
	                     <%} %>
	                    <div class="box2">
                        <ul>
                        	<%if (admin){ %>
                           <li><a href="/tamirandevyatar/allCourses">Show courses</a></li>
                            <li><a href="/tamirandevyatar/addCourse">Add course</a></li>
                            <li><a href="/tamirandevyatar/users">Manage users</a></li>
                            <li><a href="/tamirandevyatar/update">Update my details</a></li>
                            <%}else{ %>
                            <li><a href="/tamirandevyatar/allCourses">Show courses</a></li>
                            <li><a href="/tamirandevyatar/search">Search courses</a></li>
                            <li><a href="/tamirandevyatar/timetable">My timetable</a></li>
                            <li><a href="/tamirandevyatar/update">Update my details</a></li>
                            <li><a href="/tamirandevyatar/users">View users</a></li>
                            <%} %>
                        </ul>
                        </div>
	                </div>		
	            </div><!-- sidebar -->    	              
	            <div id="content">
	                <div id="contheader">
	                    <h2>Manage Users</h2>
	                </div>		
	                <div id="content2" class="welcome">	
						<!-- buttons --> 
						<table>
						<tr class="buttons">
						<% 
						for(int i=0;i<letters.length;i++)
						{
							String line = (i==letters.length-1) ? "" : " | ";
						%>
						<td>
						<h4 onclick="buttonClick('#<%=letters[i]%>button','<%=letters[i]%>')" id="<%=letters[i]%>button"><%=letters[i]+"("+userCount.get(letters[i])+")" %> <%= line%> </h4>
						</td>
						<%
						if (i == letters.length/2-1)
						{
							%>
							</tr><tr class="buttons">
							<%
						}
						}
						%>
						</tr>
						</table>
						<div class="entry" id="users">
						<!-- users -->  
						</div>
	                </div><!-- content2 -->								              
	            </div><!-- content -->                    
	            <div class="clearing">&nbsp;</div> 
	            <div id="footer">
	                <p><a href="http://www.cs.technion.ac.il/~cs236369/">Managing Data on the World-Wide Web</a></p>
	            </div>
	        </div><!-- main -->
	    </div><!-- page -->
	</body>
</html>